Skip to content

Keep server-named downloads inside the temporary directory - #9312

Open
garciadias wants to merge 2 commits into
devfrom
download-remote-filename-confine
Open

garciadias wants to merge 2 commits into
devfrom
download-remote-filename-confine

Conversation

@garciadias

@garciadias garciadias commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When download_and_extract(url, output_dir=...) is called without filepath, it downloads the archive into a temporary directory under the file name the server reports (Content-Disposition, or the last part of the URL). That name was joined to the temporary directory unchanged, so a name containing .. components or an absolute path resolved outside the temporary directory and the downloaded file was moved there.

This PR:

  • requires the reported name to be a single path component, and raises ValueError before anything is downloaded when it is not: a name containing / or \ (for example ../a.zip, a/b.zip, /abs/a.zip), and a name that is empty, . or ..;
  • keeps a realpath check that the resolved destination is directly inside the temporary directory;
  • asks the server for the file name once, so the extension check and the download destination use the same response (previously two HEAD requests were made).

An explicit filepath behaves as before, including a nested destination. All built-in callers (MedNISTDataset, DecathlonDataset, the MMAR and TCIA helpers, BundleGen) pass filepath, so their behaviour does not change. The only behaviour change is for a caller that omits filepath and downloads from a server whose reported name contains a directory part: it now gets a ValueError instead of a file stored under the flattened name.

Tests

  • New tests/apps/test_download_remote_filename.py (HTTP mocked, no network): ../, ../../, ..\, sub/archive.zip, sub/.. and an absolute name are all rejected with nothing downloaded and nothing written next to the temporary directory; names with no usable base name (.., ., /, //) are rejected; a plain name round trip, an explicit nested filepath, and a single HEAD request are also covered. 11 of the 13 tests fail on dev; the round trip and explicit-filepath controls pass on both.
  • Re-verified on a second machine (Python 3.12, CUDA host): 13 passed on this branch, 11 failed on dev.
  • tests/apps/test_download_and_extract.py passes locally (13 passed).

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review.

Walkthrough

When filepath is omitted, download_and_extract validates the remote filename before using it as a temporary download destination. It retrieves the remote filename once. Tests cover invalid names, successful extraction, request count, and the unchanged behavior when filepath is supplied.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: Minimal · previous review 84888

Downloads that rely on a server-reported filename are now confined to the temporary directory, and unsafe names are rejected before any download starts. Behavior with an explicit filepath is unchanged. No merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: server-named downloads are kept inside the temporary directory. It matches the raw summary and the PR objectives, so it is specific and accurate.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description covers the problem, the fix, the behaviour change for callers that omit filepath, and the test evidence. It lists the template's Description and Types of changes sections. It leaves th…
Full details: Description check

Explanation

The description covers the problem, the fix, the behaviour change for callers that omit filepath, and the test evidence. It lists the template's Description and Types of changes sections. It leaves the 'New tests added' box ticked by the author, which matches the added test file. Unchecked items (integration tests, quick tests, docs) are not confirmed as run, but the template treats these as checklist items. The description is mostly complete.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 7c00fe0 and 84888ed.

Files selected for processing: 2

  • monai/apps/utils.py
  • tests/apps/test_download_remote_filename.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

…tory

download_and_extract() without filepath now rejects a file name reported by
the server that is not a single path component (it contains a path
separator, or is empty, '.' or '..'), before anything is downloaded. Callers
that need a nested destination pass filepath explicitly. The server is asked
for the name once, so the extension check and the destination use the same
response.

Signed-off-by: R. Garcia-Dias <rafaelagd@gmail.com>
@garciadias
garciadias force-pushed the download-remote-filename-confine branch from 7c00fe0 to 84888ed Compare November 23, 2026 17:36

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant